#include "Heart.h"
#include "Resource.h"

Heart::Heart(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State) : Object(x, y)
{
	this->setIDObjType(ID);

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
	p_heart = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[8]->getFileName()), 125, 0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	this->pos_x_old = x;
	this->pos_y_old = y;

	veloc_y = 0;
	accel_y = 0;

	min_X = x - 16;
	max_X = x + 16;

	//castle
	this->obj_state = IS_IDLE;
}

Heart::~Heart()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Heart::render(int cameraX, int cameraY)
{
	p_sprite->render(pos_x, pos_y, cameraX, cameraY);
}

void Heart::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch (obj_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		{
			Object* obj;
			list<Object*>::iterator i;
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();
			pos_x_old = pos_x;
			pos_y_old = pos_y;

			pos_x += veloc_x * gameTime;
			//veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime;// + 1/2 * accel_y * gameTime *gameTime;

			if (pos_x <= min_X || pos_x >= max_X)
			{
				veloc_x *= -1;
			}

			for (i = obj_list->begin(); i != obj_list->end(); i++)
			{
				obj = *i;
				if (obj->getObjectType() == OBJECT_LAND)
				{

					Box box(this->pos_x_old,
						this->pos_y_old - this->height, 
						width, 
						height, 
						0, 
						veloc_y * gameTime);// + 1.0f/2.0f * accel_y * gameTime * gameTime);


					Box block(obj->getPosX(),
						obj->getPosY() - obj->getHeight(),
						obj->getWidth(),
						obj->getHeight()
						);

					Box boxphase = Collision::GetSweptBroadphaseBox(box);
					if (Collision::AABBCheck(boxphase, block))
					{
						float normalx = 0;
						float normaly = 0;
						float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
						if (0 < collision_time && collision_time < 1)
						{
							pos_y = pos_y_old + veloc_y * collision_time;// + 1.0f/2.0f * accel_y * collision_time *collision_time;// + 0.5;
							accel_y = 0;
							veloc_y = 0;
							veloc_x = 0;
						}
					}
				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;

		}
		break;
	}

	//update sprite
	p_sprite->update(gameTime);
}

OBJECT_TYPE Heart::getObjectType()
{
	return OBJECT_ITEM;
}

void Heart::changeState(int obj_state)
{
	this->obj_state = obj_state;
	switch (this->obj_state)
	{
	case IS_ACTIVING:
		veloc_x = HEART_VX;

		//accel_y = GRAVITY;
		veloc_y = -HEART_GRAVITY*100;

		p_sprite = p_heart;
		p_sprite->setFrame(0,0);


		break;
	}
}